package com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid

import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.fastjson.JSON
import com.example.library_common.album.utils.Utils
import com.example.library_common.base.BaseFragmentEx
import com.example.library_common.bean.CommonBean
import com.example.library_common.networking.RestClient
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.bean.SecKillLabBean
import com.example.shushiyunsst.util.SEC_KILL
import com.google.android.material.tabs.TabLayout
import com.senon.modularapp.util.JssSpannableString
import ikidou.reflect.TypeBuilder

/**
 * 秒杀
 * */
@Route(path = SEC_KILL)
class SecKillFragment : BaseFragmentEx(), TabLayout.OnTabSelectedListener,
    ViewPager.OnPageChangeListener {

    companion object {
        fun newInstance(): SecKillFragment {
            val args = Bundle()

            val fragment = SecKillFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private lateinit var mTabLayout: TabLayout

    private lateinit var mViewPager: ViewPager

    private val pageLs = mutableListOf<SecKillSubFragment>()

    private lateinit var triangle: ImageView


    override fun getLayoutId() = R.layout.miao_sha_fragment

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val mToolbar: Toolbar = view.findViewById(R.id.mToolbar)
        mToolbar.setTitle(R.string.sec_kill_time)
        mToolbar.setNavigationOnClickListener { onBackPressedSupport() }
        triangle = view.findViewById(R.id.triangle)
        mTabLayout = view.findViewById(R.id.mTabLayout)
        mViewPager = view.findViewById(R.id.mViewPager)

        mViewPager.adapter = object :
            FragmentStatePagerAdapter(
                childFragmentManager,
                BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
            ) {

            override fun getCount() = pageLs.size

            override fun getItem(position: Int) = pageLs[position]

            override fun getPageTitle(position: Int): CharSequence? {
                val walletLabInfo = getItem(position)
                val labBean = walletLabInfo.getLabBean()

                return JssSpannableString(_mActivity, labBean.toString())
                    .first(labBean.skpDesc)
                    .size(16)
                    .bold()
                    .first(labBean.statusDesc)
                    .size(12)
            }

        }

        mTabLayout.setupWithViewPager(mViewPager)

        requireData()


        mTabLayout.addOnTabSelectedListener(this)
        mViewPager.addOnPageChangeListener(this)
    }

    private fun requireData() {

        RestClient.builder()
            .url(R.string.queryTopSpikeList)

            .success { parseQueryTopSpikeList(it) }
            .error { code, msg ->

            }
            .failure {

            }.build().post()
    }

    private fun parseQueryTopSpikeList(it: String?) {
        val obj = JSON.parseObject(it)
        val topList = obj["topList"]
        val array = JSON.parseArray(topList.toString(), SecKillLabBean::class.java)
        pageLs.clear()
        array.forEach {
            val sub = SecKillSubFragment.newInstance()
            sub.setLabBean(it)
            pageLs.add(sub)
        }

        mViewPager.adapter?.notifyDataSetChanged()
    }

    override fun onBackPressedSupport(): Boolean {
        pop()
        return true
    }

    override fun onTabSelected(tab: TabLayout.Tab?) {


    }


    override fun onTabUnselected(tab: TabLayout.Tab?) {

    }

    override fun onTabReselected(tab: TabLayout.Tab?) {

    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

    }

    override fun onPageSelected(position: Int) {

    }

    override fun onPageScrollStateChanged(state: Int) {

    }
}